package com.jr.mapper;

import com.jr.pojo.Review;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface ReviewMapper {

    @Select("SELECT * FROM reviews WHERE review_id = #{reviewId}")
    Review selectById(Integer reviewId);

    @Select("SELECT * FROM reviews WHERE order_id = #{orderId}")
    Review selectByOrderId(Integer orderId);

    @Select("SELECT * FROM reviews WHERE provider_id = #{providerId} ORDER BY created_at DESC")
    List<Review> selectByProviderId(Integer providerId);

    @Select("SELECT * FROM reviews WHERE owner_id = #{ownerId} ORDER BY created_at DESC")
    List<Review> selectByOwnerId(Integer ownerId);

    @Insert("INSERT INTO reviews (order_id, owner_id, provider_id, rating, comment, photos, created_at) " +
            "VALUES (#{orderId}, #{ownerId}, #{providerId}, #{rating}, #{comment}, #{photos}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "reviewId")
    int insertReview(Review review);

    @Update("UPDATE reviews SET rating=#{rating}, comment=#{comment}, photos=#{photos} WHERE review_id=#{reviewId}")
    int updateReview(Review review);

    @Delete("DELETE FROM reviews WHERE review_id = #{reviewId}")
    int deleteReview(Integer reviewId);

    @Select("SELECT AVG(rating) FROM reviews WHERE provider_id = #{providerId}")
    Double selectAverageRatingByProviderId(Integer providerId);

    @Select("SELECT COUNT(*) FROM reviews WHERE provider_id = #{providerId}")
    Integer selectReviewCountByProviderId(Integer providerId);
}
